﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using CarolLib;

namespace MobileManager.Mini
{
    public partial class MonitorIndex : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDdlSP(ref ddlSP);
                ddlSP.Items.Insert(0, new ListItem("全部", "-1"));

                inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();
            }
        }

        #region DDL Bind
        private void BindDdlSP(ref DropDownList ddl)
        {
            ddl.DataSource = DBUtility.SqlHelperCarol.ExecuteDataSet("SELECT SPCode,SPDesc FROM T_SPInfo WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "SPCode";
            ddl.DataTextField = "SPDesc";
            ddl.DataBind();
        }
        #endregion

        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = @"SELECT A.ChannelID,A.ChannelName,A.CstCode,A.SPcode,A.ChannelNumber,A.ChannelOrder,A.ReceiveChannelNumber,A.ReceiveChannelOrder,A.Status,A.ChargeType,
                            B.SendCount,B.ReceiveCount,B.RateInfo,C.SPDesc FROM  T_ChannelMini A WITH(NOLOCK) LEFT JOIN T_ChannelMiniExt B WITH(NOLOCK) ON A.ChannelID=B.ChannelID AND B.DayInfo=@DayInfo  LEFT JOIN T_SPInfo C WITH(NOLOCK) ON A.SPcode=C.SPcode WHERE 1=1 ";

            List<SqlParameter> plist = new List<SqlParameter>();
            plist.Add(SqlHelper.CreateInParameter("@DayInfo", SqlDbType.Date, 10, inputDateFrom.Value));

            if (!"-1".Equals(ddlSP.SelectedValue))
            {
                strSQL += " AND A.SPcode=@SPcode";
                plist.Add(SqlHelper.CreateInParameter("@SPcode", SqlDbType.VarChar, 100, ddlSP.SelectedValue));
            }
            if (!"-1".Equals(ddlODEP.SelectedValue))
            {
                strSQL += " AND A.CstCode=@CstCode";
                plist.Add(SqlHelper.CreateInParameter("@CstCode", SqlDbType.Int, 100, ddlODEP.SelectedValue));
            }
            if (!"-1".Equals(ddlStatus.SelectedValue))
            {
                strSQL += " AND A.Status=@Status";
                plist.Add(SqlHelper.CreateInParameter("@Status", SqlDbType.Int, 100, ddlStatus.SelectedValue));
            }

            if (!string.IsNullOrEmpty(txtspnum.Text.Trim()))
            {
                strSQL += string.Format(" AND A.ChannelNumber LIKE '%{0}%'", txtspnum.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtOrder.Text.Trim()))
            {
                strSQL += string.Format(" AND A.ChannelOrder LIKE '%{1}%'", txtOrder.Text.Trim());
            }

            strSQL += " ORDER BY B.RateInfo";

            DataSet ds = DBUtility.SqlHelperCarol.ExecuteDataSet(strSQL, plist.ToArray());
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                if (lbStatus.Text.Trim().Equals("1"))
                    lbStatus.Text = "禁用";
                else if (lbStatus.Text.Trim().Equals("0"))
                    lbStatus.Text = "启用";
                else if (lbStatus.Text.Trim().Equals("2"))
                    lbStatus.Text = "当天禁用";

                Label lbODEPName = (Label)e.Row.FindControl("lbODEPName");
                if (lbODEPName.Text.Trim().Equals("1"))
                    lbODEPName.Text = "移动";
                else if (lbODEPName.Text.Trim().Equals("2"))
                    lbODEPName.Text = "联通";

                Label lbRateInfo = (Label)e.Row.FindControl("lbRateInfo");
                lbRateInfo.Text = lbRateInfo.Text.Trim() + "%";

                Label lbChargeType = (Label)e.Row.FindControl("lbChargeType");
                if (lbChargeType.Text.Trim().Equals("1"))
                    lbChargeType.Text = "短信";
                else if (lbChargeType.Text.Trim().Equals("2"))
                    lbChargeType.Text = "IVR";

                //Label lbChannelNumber = (Label)e.Row.FindControl("lbChannelNumber");
                Label lbChannelOrder = (Label)e.Row.FindControl("lbChannelOrder");
                //Label lbReceiveChannelNumber = (Label)e.Row.FindControl("lbReceiveChannelNumber");
                Label lbReceiveChannelOrder = (Label)e.Row.FindControl("lbReceiveChannelOrder");

                if (lbChannelOrder.Text.Trim().Length > 20)
                {
                    lbChannelOrder.Text = lbChannelOrder.Text.Trim().Substring(0, 20);
                }
                if (lbReceiveChannelOrder.Text.Trim().Length > 20)
                {
                    lbReceiveChannelOrder.Text = lbReceiveChannelOrder.Text.Trim().Substring(0, 20);
                }
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;

                string strReceiveChannelNumber = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtReceiveChannelNumber")).Text.Trim();
                string strReceiveChannelOrder = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtReceiveChannelOrder")).Text.Trim();
                if (string.IsNullOrEmpty(strReceiveChannelNumber))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "返回号码不能为空！");
                    return;
                }

                string strSQL = string.Format("UPDATE T_ChannelMini SET ReceiveChannelNumber='{0}',ReceiveChannelOrder='{1}' WHERE ChannelID={2}", strReceiveChannelNumber, strReceiveChannelOrder, strID);
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(inputDateFrom.Value.Trim()))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "日期不能为空！");
                    return;
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}